package com.hisan.freeride.common.view;

import android.app.TimePickerDialog;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TimePicker;

import java.util.Date;

public class MyTimePicker extends TimePickerDialog{
	private boolean isSet;
	private int hour,minute;
	public MyTimePicker(Context context, OnTimeSetListener callBack,
			int hourOfDay, int minute, boolean is24HourView,boolean isSet) {
		super(context, callBack, hourOfDay, minute, is24HourView);
		this.isSet = isSet;
		this.hour = hourOfDay;
		this.minute = minute;
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		Date time = new Date();		//目前日期
		Date nextTime = new Date();		//将要设置的日期，把值传入其中
		nextTime.setHours(hourOfDay);
		nextTime.setMinutes(minute);
		if(view != null){
			if(!isSet){
				Log.i("test","日期等于今天，时间有限制");
					if(nextTime.getTime() - time.getTime() <= 0){
						Log.i("test","hour1:"+hourOfDay+",minut:"+minute);
						Log.i("test","日期等于今天，时间小于等于现在，不能按 - 号按钮");
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(2).setEnabled(false);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(2).setEnabled(false);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(1).setEnabled(false);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(1).setEnabled(false);
						super.onTimeChanged(view, hourOfDay, minute);
					}else{
//						Log.i("test","日期等于今天，时间大于现在，可以按 - 号按钮");
//						Log.i("test","hour2:"+time.getHours()+",minut:"+time.getMinutes());
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(2).setEnabled(true);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(2).setEnabled(true);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(1).setEnabled(true);
						((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(1).setEnabled(true);
						this.hour = hourOfDay;
						this.minute = minute;
						super.onTimeChanged(view, hourOfDay, minute);
					}
			}else{
				Log.i("test","日期大于今天，时间随便选");
				((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(2).setEnabled(true);
				((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(2).setEnabled(true);
				((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(0)).getChildAt(1).setEnabled(true);
				((ViewGroup) ((ViewGroup) view.getChildAt(0)).getChildAt(1)).getChildAt(1).setEnabled(true);
				this.hour = hourOfDay;
				this.minute = minute;
				super.onTimeChanged(view, hourOfDay, minute);
			}
		}
	}
	
	public void setIsSet(boolean isSet){
		this.isSet = isSet;
	}
	
	public void setHour(int hour) {
		this.hour = hour;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public int getHour(){
		return hour;
	}
	
	public int getMinute(){
		return minute;
	}
//	
//	public Date getTime(){
//		return new Date();
//	}
}
